package com.cloud.core.tools;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.cloud.core.R;
import com.cloud.core.app.BaseApplication;
import com.cloud.widget.utils.DensityUtils;


/**
 * Created by Tanghaibo
 * 2018/3/21
 * description:
 */

public final class ToastManager {
    private Toast mToast;
    private final BaseApplication mApplication;
    private Runnable mRunnable;
    private static Handler sHandler = new Handler(Looper.getMainLooper());

    public ToastManager(BaseApplication application) {
        mApplication = application;
    }

    public Toast shortToast(final String message) {
        cancel();
        removeCallback();

        mRunnable = new Runnable() {
            @Override
            public void run() {
                mToast = new Toast(mApplication);
                mToast.setGravity(Gravity.CENTER, 0, 0);
                View view = LayoutInflater.from(mApplication).inflate(R.layout.layout_toast, null);
                TextView tvMessage = (TextView) view.findViewById(R.id.id_tv_toast);
                //设置文本
                tvMessage.setText(TextUtils.isEmpty(message) ? "" : message);
                //设置视图
                mToast.setView(view);
                //设置显示时长
                mToast.setDuration(Toast.LENGTH_SHORT);
                //显示
                mToast.show();
                removeCallback();
            }
        };
        sHandler.post(mRunnable);
        return mToast;
    }

    public Toast alertToast(final String message, @ColorRes int background, @DrawableRes int icon) {
        cancel();
        removeCallback();

        mRunnable = new Runnable() {
            @Override
            public void run() {
                mToast = new Toast(mApplication);
                mToast.setGravity(Gravity.TOP, 0, 0);
                View view = LayoutInflater.from(mApplication).inflate(R.layout.layout_toast, null);
                TextView tvMessage = (TextView) view.findViewById(R.id.id_tv_toast);
                ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_toast);
                LinearLayout llToast = (LinearLayout) view.findViewById(R.id.ll_toast);
                llToast.setBackgroundColor(ContextCompat.getColor(mApplication, background));
                ivIcon.setImageResource(icon);
                ivIcon.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.getDeviceWidth(mApplication), ViewGroup.LayoutParams.WRAP_CONTENT);
                llToast.setLayoutParams(params);
                //设置文本
                tvMessage.setText(TextUtils.isEmpty(message) ? "" : message);
                //设置视图
                mToast.setView(view);
                //设置显示时长
                mToast.setDuration(Toast.LENGTH_SHORT);
                //显示
                mToast.show();
                removeCallback();
            }
        };
        sHandler.post(mRunnable);
        return mToast;
    }

    public void success(final String message) {
        alertToast(message, R.color.successColor, R.drawable.ic_sneaker_success);
    }

    public void info(final String message) {
        alertToast(message, R.color.infoColor, R.drawable.ic_sneaker_error);
    }

    public void warn(final String message) {
        alertToast(message, R.color.warningColor, R.drawable.ic_sneaker_success);
    }

    public void error(final String message) {
        alertToast(message, R.color.errorColor, R.drawable.ic_sneaker_warning);
    }

    public void success(final int message) {
        alertToast(mApplication.getString(message), R.color.successColor, R.drawable.ic_sneaker_success);
    }

    public void info(final int message) {
        alertToast(mApplication.getString(message), R.color.infoColor, R.drawable.ic_sneaker_error);
    }

    public void warn(final int message) {
        alertToast(mApplication.getString(message), R.color.warningColor, R.drawable.ic_sneaker_success);
    }

    public void error(final int message) {
        alertToast(mApplication.getString(message), R.color.errorColor, R.drawable.ic_sneaker_warning);
    }

    public Toast shortToast(final int message) {
        return shortToast(mApplication.getString(message));
    }

    public Toast longToast(final String message) {
        cancel();
        removeCallback();

        mRunnable = new Runnable() {
            @Override
            public void run() {
                mToast = Toast.makeText(mApplication, message, Toast.LENGTH_LONG);
                mToast.show();
                removeCallback();
            }
        };
        sHandler.post(mRunnable);
        return mToast;
    }

    public Toast longToast(final int message) {
        return longToast(mApplication.getString(message));
    }

    public Toast centerShortToast(final String message) {
        cancel();
        removeCallback();

        mRunnable = new Runnable() {
            @Override
            public void run() {
                mToast = Toast.makeText(mApplication, message, Toast.LENGTH_LONG);
                mToast.setGravity(Gravity.CENTER, 0, 0);
                mToast.show();
                removeCallback();
            }
        };
        sHandler.post(mRunnable);
        return mToast;
    }

    private void cancel() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }

    private void removeCallback() {
        if (mRunnable != null) {
            sHandler.removeCallbacks(mRunnable);
            mRunnable = null;
        }
    }
}
